package com.piggy.framework.local;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.piggy.framework.local.AddressTask;
//import com.piggy.framework.gps.GpsActivity;
import com.piggy.framework.local.GpsTask.GpsData;
import com.piggy.framework.local.IAddressTask.MLocation;
import android.content.Context;
import android.app.Activity;
import android.os.Message;

/*
 gps,apn,wifi瀹氫綅
 */
public class GetLocalInformation
{
	protected Activity c;
	
	protected GpsHandler handler;
	protected Handler preHandler;
	//protected Handler handler;
	
	public GetLocalInformation(GpsHandler h,Handler preh)
	{
		handler=h;
		preHandler=preh;
	}
	
	public void setHandler(GpsHandler h) 
	{
		handler=h;		
	}
	
	public void setPreHandler(Handler preh)
	{
		preHandler=preh;
	}
	
	//寮�鏄剧ず鎵ц鎻愮ず
	public void showProgress() 
	{
		
	}
	
	//缁撴潫鎻愮ず
	public  void hiddenProgress() 
	{
		
	}
	
	//apn瀹氫綅
	private void do_apn() 
	{
		new AsyncTask<Void, Void, MLocation>() 
		{
			@Override
			protected MLocation doInBackground(Void... params) 
			{
				MLocation location = null;
				try 
				{
					location = new AddressTask(c,
							AddressTask.DO_APN).doApnPost();
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
				if(location == null)
					return null;
				return location;
			}

			@Override
			protected void onPreExecute() {
				//dialog.show();
				showProgress();
				super.onPreExecute();
			}

			@Override
			protected void onPostExecute(MLocation result) 
			{
				hiddenProgress();
				
				Message msg=new Message();
				msg.what=0;
				Bundle bundle=new Bundle();
				GpsResult r=new GpsResult();
				r.setLocation(result);
				bundle.putSerializable("location", r);
				msg.setData(bundle);
				handler.sendMessage(msg);
				
				super.onPostExecute(result);
			}
			
		}.execute();
	}

	//gps瀹氫綅
	private void do_gps(final GpsData gpsdata) 
	{
		new AsyncTask<Void, Void, MLocation>() {

			@Override
			protected MLocation doInBackground(Void... params) 
			{
				MLocation location = null;
				try {
					Log.i("do_gpspost", "缁忕含搴︼細" + gpsdata.getLatitude() + "----" + gpsdata.getLongitude());
					location = new AddressTask(c,
							AddressTask.DO_GPS).doGpsPost(gpsdata.getLatitude(),
									gpsdata.getLongitude());
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
				if(location == null)
					return null;
					//return "GPS淇℃伅鑾峰彇閿欒";
				return location;
			}

			@Override
			protected void onPreExecute() 
			{
				//dialog.show();
				showProgress();
				super.onPreExecute();
			}

			@Override
			protected void onPostExecute(MLocation result) 
			{
				hiddenProgress();
				//杩斿洖瀹氫綅缁撴灉				
				Message msg=new Message();
				msg.what=0;
				Bundle bundle=new Bundle();
				GpsResult r=new GpsResult();
				r.setLocation(result);
				bundle.putSerializable("location", r);
				msg.setData(bundle);
				handler.sendMessage(msg);
				
				super.onPostExecute(result);
			}
			
		}.execute();
	}

	//wifi瀹氫綅
	private void do_wifi() 
	{
		new AsyncTask<Void, Void, MLocation>() 
		{
			@Override
			protected MLocation doInBackground(Void... params) 
			{
				MLocation location = null;
				try 
				{
					location = new AddressTask(c,
							AddressTask.DO_WIFI).doWifiPost();
				} 
				catch (Exception e) 
				{
					e.printStackTrace();
				}
				if(location == null)
					return null;
				return location;
			}

			@Override
			protected void onPreExecute() 
			{
				//dialog.show();
				showProgress();
				super.onPreExecute();
			}

			@Override
			protected void onPostExecute(MLocation result) 
			{
				hiddenProgress();
				//杩斿洖瀹氫綅缁撴灉				
				Message msg=new Message();
				msg.what=0;
				Bundle bundle=new Bundle();
				GpsResult r=new GpsResult();
				r.setLocation(result);
				bundle.putSerializable("location", r);
				msg.setData(bundle);
				handler.sendMessage(msg);
				super.onPostExecute(result);
			}
			
		}.execute();
	}
}